package cn.etynit.web.controller;

import cn.etynit.service.IActivityService;
import cn.etynit.domain.Activity;
import cn.etynit.query.ActivityQuery;
import cn.etynit.result.JsonResult;
import cn.etynit.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    public IActivityService activityService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody Activity activity){
        if(activity.getId()!=null){
            activityService.updateById(activity);
        }else{
            activityService.insert(activity);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        activityService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        activityService.selectById(id);
        return JsonResult.success();
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(activityService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody ActivityQuery query){
        Page<Activity> page = new Page<Activity>(query.getPage(),query.getRows());
        page = activityService.selectPage(page);
        return JsonResult.success(new PageList<Activity>(page.getTotal(),page.getRecords()));
    }

    /**
     * 根据活动标题查询活动是否存在
     * @param title
     * @return
     */
    @RequestMapping(value = "/findByTitle/{title}",method = RequestMethod.GET)
    public JsonResult findByTitle(@PathVariable("title")String title){
        activityService.findByTitle(title);
        return JsonResult.success();
    }
}
